<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/11/2
 * Time: 20:10
 */

namespace app\admin\service;

use app\models\CourseModel;
use app\models\CoursePackageModel;
use app\models\PackageModel;

class PackageService
{
    /**
     * @return array
     */
    public function package_manage()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $course_name = input('course_name', '');
            $course_ascription = input('course_ascription', '');
            $year = input('year', '');
            $grade = input('grade', '');
            $where = [];
            if ($course_name) $where['name'] = ['like', '%' . $course_name . '%']; /*搜索*/
            if ($course_ascription) $where['course_ascription'] = $course_ascription;
            if ($year) $where['year'] = $year;
            if ($grade) $where['grade'] = $grade;
            $order = 'id desc';
            $customers = PackageModel::pageLists($where, $page, $listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @param $type
     * @return array
     */
    public function logic($type)
    {
        try {
            $user = session('user');
            $data = input('post.');
            $data['course_ascription'] = $user['course_ascription'];
            $data['founder'] = $user['name'];
            $data['totalprice'] = $data['totalprice'] * 100;
            $data['discount'] = $data['discount'] * 100;
            $data['receivable'] = $data['receivable'] * 100;
            $data['create_date'] = date('Y-m-d');
            if ($type == 1) {
                PackageModel::create($data);
                return ['code' => 0, 'msg' => '新建课程包成功'];
            } else if ($type == 2) {
                PackageModel::update($data);
                return ['code' => 0, 'msg' => '修改课程包成功'];
            }
        } catch (\Exception $ex) {
            if (($ex->getCode()) == 10501) {
                return ['code' => 1, 'msg' => '此课程包已经存在'];
            }
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    public function course()
    {
        $courses = input('course/a');
        $totalprice = '';
        if (count($courses) > 1) {
            foreach ($courses as $value) {
                $totalprice += CourseModel::where('id', $value)->value('total_price');
            }
        } else if(count($courses) == 1) {
            foreach ($courses as $value) {
                $totalprice = CourseModel::where('id', $value)->value('total_price');
            }
        }
        $data['totalprice'] = $totalprice;
        if ($courses == 'null' || !$courses) {
            return ['code' => 1, 'msg' => "请至少选一个课程"];
        } else {
            $data['package'] = implode(',', $courses);
        }
        return ['code' => 0, 'totalprice' => $data['totalprice'], 'package' => $data['package']];
    }

    /**
     * 改变状态
     * @return array
     */
    public function package_check_status()
    {
        try {
            $data = input('post.');
            if ($data['status'] == 0) {
                $msg = '禁用';
            } else {
                $msg = '启用';
            }
            PackageModel::update($data);
            return ['code' => 0, 'msg' => $msg.'成功'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}



